package xyz.penghaonan.wificonnect.connect;

import android.os.Handler;
import android.os.Looper;

import com.penghaonan.appframework.utils.WeakObservableList;

import xyz.penghaonan.wificonnect.WifiConnectSDK;
import xyz.penghaonan.wificonnect.sockethelper.BaseSocketHelper;

abstract public class BaseConnectHelper implements IConnectHelper {
    protected static final int TIME_OUT = 10000;
    private int state = STATE_OFF;
    private final WeakObservableList<OnConnectStateChangedListener> stateObserver = new WeakObservableList<>();
    protected Handler handler = new Handler(Looper.getMainLooper());

    public void addOnDeviceConnectStateListener(OnConnectStateChangedListener listener) {
        boolean added = stateObserver.addListener(listener);
        if (added) {
            listener.onStateChanged(getState());
        }
    }

    public void removeOnDeviceConnectStateListener(OnConnectStateChangedListener listener) {
        stateObserver.removeListener(listener);
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public int getState() {
        return state;
    }

    abstract public BaseSocketHelper getSocketHelper();

    @Override
    public String getStateDesc() {
        String desc = "";
        switch (state) {
            case STATE_OFF:
                desc = "已关闭";
                break;
            case STATE_SOCKET_CONNECTED:
                desc = "已连接";
                break;
            case STATE_SOCKET_CREATE:
                desc = "创建连接";
                break;
            case STATE_SOCKET_WAITING:
                desc = "等待接入";
                break;
            case STATE_WIFI_CONNECTED:
                desc = "网络已连接";
                break;
            case STATE_WIFI_CONNECTING:
                desc = "网络正在连接";
                break;
            case STATE_WIFI_LISTENING:
                desc = "网络未连接";
                break;
            case STATE_SOCKET_CONNECTING:
                desc = "连接中";
        }
        return desc;
    }

    protected void setState(int state) {
        if (this.state == state) {
            return;
        }
        this.state = state;
        if (state == STATE_OFF) {
            WifiConnectSDK.getServiceHelper().stop();
        } else {
            WifiConnectSDK.getServiceHelper().start();
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                stateObserver.iterate(new WeakObservableList.OnIterator<OnConnectStateChangedListener>() {
                    @Override
                    protected boolean onIterator(OnConnectStateChangedListener item) {
                        item.onStateChanged(getState());
                        return false;
                    }
                });
            }
        });
    }

    protected boolean isWifiConnectedState() {
        return getState() != STATE_WIFI_LISTENING && getState() != STATE_WIFI_CONNECTING && getState() != STATE_OFF;
    }
}
